package com.example.myone.sql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.myone.Mapper.ArticleMapper;
import com.example.myone.Mapper.UserMapper;

public class MySQLiteHelper extends SQLiteOpenHelper {
    private static final String CREATE_ARTICLE
            = "create table article" +
            "(id integer primary key autoincrement," +
            "title text," +
            "author text," +
            "category text," +
            "introduction text," +
            "content text," +
            "cover text," +
            "likes integer," +
            "create_time text," +
            "author_id integer)";

    private static final String CREATE_USER
            = "create table user" +
            "(id integer primary key autoincrement," +
            "account text," +
            "password text," +
            "username text," +
            "avatar text," +
            "background text)";

    private static final String CREATE_USER_ARTICLE_LIKES =
            "create table user_article_like" +
            "(id integer primary key autoincrement," +
            "user_id integer," +
            "article_id integer)";
    private static final String CREATE_MUSIC =
            "create table music" +
            "(id integer primary key autoincrement," +
            "article_id integer," +
            "bytes blob)";
    public MySQLiteHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_ARTICLE);
        db.execSQL(CREATE_USER);
        db.execSQL(CREATE_USER_ARTICLE_LIKES);
        db.execSQL(CREATE_MUSIC);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists article");
        db.execSQL("drop table if exists user");
        db.execSQL("drop table if exists user_article_like");
        db.execSQL("drop table if exists music");
        onCreate(db);
    }
    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists article");
        db.execSQL("drop table if exists user");
        db.execSQL("drop table if exists user_article_like");
        db.execSQL("drop table if exists music");
        onCreate(db);
    }
    public static MySQLiteHelper getMySQLHelper(Context context) {
        return new MySQLiteHelper(context, "one.db", null, 1);
    }
    public static SQLiteDatabase getReadableSQLiteDatabase(Context context) {
        MySQLiteHelper MySQLiteHelper = getMySQLHelper(context);
        return MySQLiteHelper.getReadableDatabase();
    }
    public static SQLiteDatabase getWritableDatabase(Context context) {
        MySQLiteHelper MySQLiteHelper = getMySQLHelper(context);
        return MySQLiteHelper.getWritableDatabase();
    }

}
